package com.itheima.common;

import com.itheima.exception.DisableException;
import com.itheima.exception.FileException;
import com.itheima.exception.RepeatException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;
@Slf4j
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionHandler {
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R doHandler(SQLIntegrityConstraintViolationException e){
        log.error("异常信息：{}",e.getMessage());
        if(e.getMessage().contains("Duplicate entry")){
            String msg = e.getMessage().split(" ")[2]+"已经存在";
            return R.error(msg);
        }
        return R.error("未知错误");
    }

    @ExceptionHandler(DisableException.class)
    public R doHandler1(DisableException e){
        log.error("异常信息：{}",e.getMessage());
        return R.error(e.getMessage());
    }
    @ExceptionHandler(RepeatException.class)
    public R doHandler2(RepeatException e){
        log.error("异常信息：{}",e.getMessage());
        return R.error(e.getMessage());
    }

    @ExceptionHandler(FileException.class)
    public R doHandler2(FileException e){
        log.error("异常信息：{}",e.getMessage());
        return R.error(e.getMessage());
    }


}
